{{from uliweb.utils.textconvert import text2html}}
{{for p in sorted(catalogs.keys(), key=str.lower):}}
    {{v = catalogs[p]}}
    <div class="apps_panel">
    <h2>{{=p}}</h2>
    {{for d in sorted(v, key=lambda x:x['name'].lower()):}}
        <div class="app_panel">
            {{if d['selected']:}}
                <h3 class="app_name selected">
                    <input type="checkbox" name="appname" class="f_checkbox" id="check_{{=d['module']}}" checked/> 
                    <a>{{=d['name']}}({{=d['module']}})</a> 
                    <span>+</span>
                </h3>
            {{else:}}
                <h3 class="app_name noselected">
                    <input type="checkbox" name="appname" class="f_checkbox" id="check_{{=d['module']}}"/> 
                    <a>{{=d['name']}}({{=d['module']}})</a> 
                    <span>+</span>
                </h3>
            {{pass}}
            <div class="details" id="panel_{{=d['module']}}" style="overflow:auto">
                <table border=0 class="full">
                    <tr>
                        <th>Configuration</th>
                        <th>Information</th>
                    </tr>
                    <tr>
                        <td width="60%">
                            <div id="form_{{=d['name']}}" class="config-body">
                            </div>
                        </td>
                        <td width="40%">
                            <div class="detail-body">
                                <!-- <img src="{{url_for_static(d['icon'])}}"/> -->
                                <ul>
                                <li><b>Name:</b> {{=d['name']}}</li>
                                <li><b>Title:</b> {{=d['title']}}</li>
                                <li><b>Author:</b> {{=d['author']}}</li>
                                <li><b>Version:</b> {{=d['version']}}</li>
                                <li><b>Homepage:</b> {{=d['homepage']}}</li>
                                <li><b>Module:</b> {{=d['module']}}</li>
                                <li><b>Description:</b> {{<< text2html(d['desc'])}}</li>
                                </ul>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    {{pass}}
    </div>
{{pass}}

<script type="text/javascript">
window.addEvent('domready', function() {
	$$('input.f_checkbox').addEvent('click', function(e){
        var parent = this.getParent('h3');
        //send message
        var action = 'del';
        if (this.checked){
            action = 'add';
        }
        var input = this;
        var req = new Request({
        	method: 'get',
        	url: "{{=url_for('plugs.develop.views.develop_edit_app')}}",
        	onComplete: function(response) {
                if (input.checked) {
                    parent.removeClass('noselected');
                    parent.addClass('selected');
                }
                else {
                    parent.removeClass('selected');
                    parent.addClass('noselected');
                }
            }
        }).send('action='+action+'&'+'module='+this.get('id').slice(6));
    });
    
    var slide = function(listtag, headingtag, collapsibles, hide){
        var list = $$(listtag);
        var headings = $$(headingtag);
        
        headings.each( function(heading, i) {
        
            var collapsible = new Fx.Slide(list[i], { 
                duration: 500, 
                transition: Fx.Transitions.linear,
                onComplete: function(){
                    if(this.open){
                        var div = list[i].getElement('div.config-body');
                        div.set('load', {'onSuccess':function(response){
                                form_send(div.getElement('form'));
                            }
                        });
                        div.load('{{=url_for("plugs.develop.views.develop_app_conf")}}?module='+list[i].get('id').slice(6));
                    }
                }
            });
            
            collapsibles[i] = collapsible;
            
            heading.addEvent('click', function(){
                var span = heading.getParent('h3').getFirst('span');
        
                if(span){
                    var newHTML = span.innerHTML == '+' ? '-' : '+';
                    span.set('html', newHTML);
                }
                
                collapsible.toggle();
                
                return false;
            });
            
            if (hide){
                collapsible.hide();
            };
        });
    };
    
    var cps = new Array();
    
    slide('div.details', '.app_panel h3 a', cps, true);
    
    //add form send event
    var form_send = function(el){
        el.addEvent('submit', function(e) {
            //Prevents the default submit event from loading a new page.
            e.stop();
            
            var b = this.getElement('.button[type=submit]');
            var div = this.getParent();
            b.set('disabled', true)
            this.set('send', {onComplete: function(response) { 
                div.set('html', response);
                form_send(div.getElement('form'))
            }});
            //Send the form.
            this.send();
        });
    };

});
</script>

